package com.mytomcat.catalina.container;


import com.mytomcat.catalina.lifecycle.LifeCycleBase;
import com.mytomcat.catalina.pipeline.Pipeline;
import com.mytomcat.catalina.pipeline.StandardPipeline;

/**
 * 容器基础类
 *
 * @author supu
 * @date 2019-04-19 11:35
 **/
public abstract class ContainerBase extends LifeCycleBase implements Container {
    protected StandardPipeline pipeline = new StandardPipeline(this);

    private Container container;

    private Container[] containers = new Container[0];
    private Object containerLock = new Object();

    @Override
    public Pipeline getPipeline(){
        return pipeline;
    }

    @Override
    protected void startInternal() {
        synchronized (containerLock) {
            for (Container container : containers) {
                container.start();
            }
        }
    }

    @Override
    public void addContainer(Container container){
        synchronized (containerLock) {
            Container[] newContainers = new Container[containers.length + 1];
            System.arraycopy(containers, 0, newContainers, 0, containers.length);
            newContainers[containers.length] = container;

            containers = newContainers;
        }
    }

    @Override
    public void setContainer(Container container){
        this.container = container;
    }

    @Override
    public Container getContainer(){
        return container;
    }
}
